package com.yizhuoyan.his.webapi.controller.admin;

import com.yizhuoyan.his.ao.ModifyRoleAo;
import com.yizhuoyan.his.ao.RoleAddAo;
import com.yizhuoyan.his.dto.JSONResponse;
import com.yizhuoyan.his.dto.QueryResult;
import com.yizhuoyan.his.entity.SysRoleEntity;
import com.yizhuoyan.his.service.admin.SysRoleManageService;
import com.yizhuoyan.his.webapi.controller.admin.vo.SysRoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


@RestController
public class SysRoleManageController {
    @Autowired
    SysRoleManageService service;
    @PostMapping("/api/sys-role/")
    public ResponseEntity<JSONResponse> addSysRole(RoleAddAo ao) throws Exception {
        SysRoleEntity e = service.addNewRole(ao);
        return ResponseEntity.ok().build();
    }
    @PutMapping("/api/sys-role/{id}")
    public ResponseEntity<Void> modifySysRole(@PathVariable Long id , ModifyRoleAo ao) throws Exception {
        service.modifyRole(id,ao);
        return ResponseEntity.ok().build();
    }
    @DeleteMapping("/api/sys-role/{id}")
    public ResponseEntity<Void> removeSysRole(@PathVariable Long id ) throws Exception {
        service.removeRole(id);
        return ResponseEntity.ok().build();
    }
    @GetMapping("/api/sys-role/{id}")
    public ResponseEntity<SysRoleVo> querySysRole(@PathVariable Long id ) throws Exception {
        SysRoleEntity e = service.queryRole(id);
        return ResponseEntity.ok(SysRoleVo.of(e));
    }
    @GetMapping("/api/sys-role/")
    public JSONResponse list(String key, String pageNo, String pageSize) throws Exception {
        if (pageNo == null)
            pageNo = "1";
        if (pageSize == null)
            pageSize = "10";
        Integer pageNoInt=Integer.parseInt(pageNo);
        Integer pageSizeInt=Integer.parseInt(pageSize);
        QueryResult<SysRoleEntity> queryResult = service.queryAllRoleByKey(key, pageNoInt, pageSizeInt);
        QueryResult<SysRoleVo> queryVoResult = queryResult.rowsMap(SysRoleVo::of);
        return JSONResponse.ok(queryVoResult);

    }

}
